package com.rxyb.toolize;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 金额处理工具
 *
 * @coder duyh,@auth duyh
 * @date May 17, 2020
 */
public class AmountUtils {

    /**
     * 加法
     *
     * @param v1
     * @param v2
     * @return
     * @author duyh
     */
    public static Integer add(Integer v1, Integer v2) {
        if (null == v1 || null == v2) {
            return null;
        }
        return new BigDecimal(v1).add(new BigDecimal(v2)).intValue();
    }

    /**
     * 减
     *
     * @param v1
     * @param v2
     * @return
     */
    public static Integer subtract(Integer v1, Integer v2) {
        if (null == v1 || null == v2) {
            return null;
        }
        return new BigDecimal(v1).subtract(new BigDecimal(v2)).intValue();
    }

    /**
     * 乘
     *
     * @param v1
     * @param v2
     * @return
     */
    public static Integer multiply(Integer v1, Integer v2) {
        if (null == v1 || null == v2) {
            return null;
        }
        return new BigDecimal(v1).multiply(new BigDecimal(v2)).intValue();
    }

    /**
     * 除法
     *
     * @param d1
     * @param d2
     * @param len 保留几位小数
     * @return
     * @author duyh
     */
    public static String div(Integer d1, Integer d2, int len) {
        if (null == d1 || null == d2) {
            return null;
        }
        double result = new BigDecimal(d1).divide(new BigDecimal(d2), len, RoundingMode.HALF_UP).doubleValue();
        return String.valueOf(result);
    }

    /**
     * 元--分
     *
     * @param yuan
     * @return
     */
    public static Integer Y2F(String yuan) {
        if (yuan == null) {
            return null;
        }
        return new BigDecimal(yuan).multiply(new BigDecimal(100)).intValue();
    }

    /**
     * 分--元
     *
     * @param fen
     * @return
     */
    public static String F2Y(Integer fen) {
        if (fen == null) {
            return null;
        }
        return new BigDecimal(fen).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP).toString();
    }

    /**
     * 分--元
     *
     * @param fen
     * @return
     */
    public static String F2Y(Long fen) {
        if (fen == null) {
            return null;
        }
        return new BigDecimal(fen).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP).toString();
    }

    private static final String fraction[] = {"角", "分"};
    private static final String digit[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String unit[][] = {{"元", "万", "亿"}, {"", "拾", "佰", "仟"}};

    /**
     * 数字金额大写转换，先写完整的然后将如零拾替换成零 要用到正则表达式
     *
     * @param amount
     */
    public static String digitUppercase(double amount) {
        String head = amount < 0 ? "负" : "";
        amount = Math.abs(amount);
        String s = "";
        for (int i = 0; i < fraction.length; i++) {
            s += (digit[(int) (Math.floor(amount * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
        }
        if (s.length() < 1) {
            s = "整";
        }
        int integerPart = (int) Math.floor(amount);
        for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
            String p = "";
            for (int j = 0; j < unit[1].length && amount > 0; j++) {
                p = digit[integerPart % 10] + unit[1][j] + p;
                integerPart = integerPart / 10;
            }
            s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
        }
        return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
    }

    public static void main(String[] args) {
        System.out.println(add(5, 4));
        System.out.println(subtract(5, 4));
        System.out.println(multiply(5, 4));
        System.out.println(div(5, 3, 2));
        System.out.println(Y2F("23"));
        System.out.println(F2Y(2345));
        System.out.println(F2Y(2345L));

        System.out.println(digitUppercase(0)); // 零元整
        System.out.println(digitUppercase(123)); // 壹佰贰拾叁元整
        System.out.println(digitUppercase(1000000)); // 壹佰万元整
        System.out.println(digitUppercase(100000001)); // 壹亿零壹元整
        System.out.println(digitUppercase(1000000000)); // 壹拾亿元整
        System.out.println(digitUppercase(1234567890)); // 壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾元整
        System.out.println(digitUppercase(1001100101)); // 壹拾亿零壹佰壹拾万零壹佰零壹元整
        System.out.println(digitUppercase(110101010)); // 壹亿壹仟零壹拾万壹仟零壹拾元整
        System.out.println();
        // 小数
        System.out.println(digitUppercase(0.12)); // 壹角贰分
        System.out.println(digitUppercase(123.34)); // 壹佰贰拾叁元叁角肆分
        System.out.println(digitUppercase(97001.34)); // 壹佰贰拾叁元叁角肆分
        System.out.println(digitUppercase(1000000.56)); // 壹佰万元伍角陆分
        System.out.println(digitUppercase(100000001.78)); // 壹亿零壹元柒角捌分
        System.out.println(digitUppercase(1000000000.90)); // 壹拾亿元玖角
        System.out.println(digitUppercase(1234567890.03)); // 壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾元叁分
        System.out.println(digitUppercase(1001100101.00)); // 壹拾亿零壹佰壹拾万零壹佰零壹元整
        System.out.println(digitUppercase(110101010.10)); // 壹亿壹仟零壹拾万壹仟零壹拾元壹角

        // 负数
        System.out.println(digitUppercase(-0.12)); // 负壹角贰分
        System.out.println(digitUppercase(-123.34)); // 负壹佰贰拾叁元叁角肆分
        System.out.println(digitUppercase(-1000000.56)); // 负壹佰万元伍角陆分
        System.out.println(digitUppercase(-100000001.78)); // 负壹亿零壹元柒角捌分
        System.out.println(digitUppercase(-1000000000.90)); // 负壹拾亿元玖角
        System.out.println(digitUppercase(-1234567890.03)); // 负壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾元叁分
        System.out.println(digitUppercase(-1001100101.00)); // 负壹拾亿零壹佰壹拾万零壹佰零壹元整
        System.out.println(digitUppercase(-110101010.10)); // 负壹亿壹仟零壹拾万壹仟零壹拾元壹角
    }
}
